﻿//************************************************************************************************
// Copyright © 2003-2012 Steven M. Cohn. All Rights Reserved.
//
// Facility:	Microsoft Development Environment 2012  Version 11.0.50727.1
// Environment:	Microsoft .NET Framework 4.5            Version v4.0.30319
//
// Description: Extends the HttpCookie class.
//
// Revision History:
// -Who--------------- -When---------- -What------------------------------------------------------
// Steven M. Cohn		18-Oct-2003		New
// Steven M. Cohn		10-Nov-2003		Added File Access methods
// Steven M. Cohn		12-Oct-2004		VS.NET 2005, .NET 2.0
// Steven M. Cohn		20-Nov-2012		Refresh! VS2010 .NET 4.5 64-bit
//************************************************************************************************

namespace CookieChipper
{
	using System;

	/*
	 * Note we used to encapsulate an HttpCookie but that was only 50% useful and didn't
	 * help with subkeys.  So we got rid of that and just use our simplified Chip concept.
	 */

	/// <summary>
	/// Provides a representation of an individual cookie.
	/// </summary>
	/// <remarks>
	/// This is an encapsulation and extension of the System.Web.HttpCookie which contains
	/// most of the properties we need.
	/// </remarks>

	internal class Cookie : Chip
	{
		private string path;
		private ChipCollection chips;


		//========================================================================================
		// Lifecycle
		//========================================================================================

		/// <summary>
		/// Initialize a new cookie instance with the given name.
		/// </summary>
		/// <param name="name"></param>

		public Cookie (HashRef hash, string name)
			: base(name)
		{
			this.Hash = hash;

			// empty name is a placeholder for a zeroed-out record in the cache
			// so ignore this and return immediately
			if (name.Length > 0)
			{
				base.Created = DateTime.MinValue;
				base.Expires = DateTime.MinValue;
				this.Accessed = DateTime.MinValue;
				this.Updated = DateTime.MinValue;

				this.chips = null;
			}
		}


		//========================================================================================
		// Properties
		//========================================================================================

		/// <summary>
		/// Gets or sets the last accessed date and time of the cookie.
		/// </summary>

		public DateTime Accessed { get; set; }


		/// <summary>
		/// Gets the address of this cookie's hash table entry within the cache file.
		/// We use this to delete or clean this cookie.
		/// </summary>

		public HashRef Hash { get; private set; }

	
		/// <summary>
		/// Gets the name of the cookie file.
		/// </summary>

		public string Filename { get; private set; }


		/// <summary>
		/// Gets or sets the size of the file in bytes.
		/// </summary>

		public int FileSize { get; set; }

		/// <summary>
		/// Gets the number of hits or uses of this cookie.
		/// </summary>

		public int Hits { get; set; }


		/// <summary>
		/// Gets or sets the virtual path to transmit with the cookie.
		/// </summary>

		public string Path
		{
			get
			{
				return path;
			}

			set
			{
				path = value;
				Filename = System.IO.Path.GetFileNameWithoutExtension(path);
			}
		}


		/// <summary>
		/// Gets a collection of values contained within the cookie.
		/// </summary>

		public ChipCollection Values
		{
			get { return chips; }
		}


		/// <summary>
		/// Gets or sets the updated date and time of the cookie.
		/// </summary>

		public DateTime Updated { get; set; }


		//========================================================================================
		// Methods
		//========================================================================================

		public void Add (Chip chip)
		{
			if (chips == null)
			{
				chips = new ChipCollection();
				this.Value = chip.Value;
				this.Expires = chip.Expires;
				this.Created = chip.Created;

				chip.Flags |= ChipFlags.Primary;
			}

			chips.Add(chip);
		}


		public void Clear ()
		{
			if (chips != null)
			{
				chips.Clear();
				chips = null;
			}

			Value = null;
		}
	}
}
